package com.redshark.wmm.UI;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.jjoe64.graphview.CustomLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewStyle;
import com.jjoe64.graphview.LineGraphView;
import com.redshark.wmm.R;
import com.redshark.wmm.MeasurementProcessing.Measurement;

public class ACFragment extends Fragment {
	GraphViewSeries emptySeries;
	GraphView graphView;
	String freqValue, freqUnits, RMSValue, RMSUnits;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		Activity a = getActivity();

		// Graphing Stuff Demo
		// init example series data
		emptySeries = new GraphViewSeries(new GraphViewData[] {});

		graphView = new LineGraphView(a // context
				, "" // heading
		);
		graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
			   @Override
			   public String formatLabel(double value, boolean isValueX) {
			      if (isValueX) {
			    	   DecimalFormat df = new DecimalFormat("0.#");
			    	   return df.format(value) + "ms";
			      }
			      return null; // let graphview generate Y-axis label for us
			   }
			});
		
		GraphViewStyle style = new GraphViewStyle();
		
		style.setVerticalLabelsWidth(90);
		
		graphView.setGraphViewStyle(style);

		// graphView.setManualYAxisBounds(5.2, 0);
		graphView.addSeries(emptySeries); // data
		
		graphView.setScrollable(true);
		// optional - activate scaling / zooming
		graphView.setScalable(true);

		// Setup Spinner
		// Spinner s = (Spinner) getView().findViewById(R.id.channelSpinner);
		// s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
		// {
		// @Override
		// public void onItemSelected(AdapterView<?> parentView,
		// View selectedItemView, int position, long id) {
		//
		// int[] scopeChannelsValue = getResources().getIntArray(
		// R.array.scope_channels_value);
		//
		// scopeChannel = scopeChannelsValue[position];
		// //Set Channel
		//
		// }
		//
		// @Override
		// public void onNothingSelected(AdapterView<?> parentView) {
		// String s = "";
		// }
		//
		// });
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		View v = inflater
				.inflate(R.layout.ac_fragment_layout, container, false);
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.graph1);
		layout.addView(graphView);
		
		//Setup zoom controls
		//ZoomControls time = (ZoomControls)v.findViewById(R.id.timeZoomControl);
		ZoomControls voltage = (ZoomControls)v.findViewById(R.id.voltageZoomControl);
		
//		time.setOnZoomInClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				((MainActivity)getActivity()).timeZoomIn();
//				
//			}
//		});
//		time.setOnZoomOutClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				((MainActivity)getActivity()).timeZoomOut();
//				
//			}
//		});
		
		voltage.setOnZoomInClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).voltageZoomIn();
				
			}
		});
		voltage.setOnZoomOutClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)getActivity()).voltageZoomOut();
				
			}
		});
		
		return v;
	}

	public void setRMSValue(Measurement m) {

		TextView valueTV = (TextView) getView().findViewById(
				R.id.rmsVoltageValueTV);
		TextView labelTV = (TextView) getView().findViewById(
				R.id.rmsVoltageUnitsTV);

    	BigDecimal bdValue = BigDecimal.valueOf(m.getValue());
    	BigDecimal wantedValue = bdValue.round(new MathContext(m.getSigFigs(), RoundingMode.HALF_UP));
    	
    	valueTV.setText(wantedValue.toEngineeringString());
    	labelTV.setText(m.getUnits());

	}

	public void setFreqValue(Measurement m) {
		TextView valueTV = (TextView) getView().findViewById(
				R.id.frequencyValueTV);
		TextView labelTV = (TextView) getView().findViewById(
				R.id.frequencyUnitsTV);

    	BigDecimal bdValue = BigDecimal.valueOf(m.getValue());
    	BigDecimal wantedValue = bdValue.round(new MathContext(m.getSigFigs(), RoundingMode.HALF_UP));
    	
    	valueTV.setText(wantedValue.toEngineeringString());
    	labelTV.setText(m.getUnits());

	}

	public void setGraphData(GraphViewSeries series) {
		graphView.removeAllSeries();
		graphView.addSeries(series);
	}
	
	public void enableScaling(boolean enable){
		ZoomControls zc = (ZoomControls) getView().findViewById(R.id.voltageZoomControl);
		if(enable){
			zc.setVisibility(View.VISIBLE);
		}
		else{
			zc.setVisibility(View.INVISIBLE);
		}
	}
}
